package com.example.campuslink.ui.activity

import android.graphics.Bitmap
import android.graphics.Color
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.widget.Toast
import com.example.campuslink.BR
import com.example.campuslink.R
import com.example.campuslink.commen.tools.Sp
import com.example.campuslink.commen.tools.SpKey
import com.example.campuslink.commen.tools.TextToImageUtil
import com.example.campuslink.commen.tools.putExtraKey
import com.example.campuslink.databinding.ActivityWordEditBinding
import com.example.campuslink.viewModel.WordEditViewModel
import java.util.Random

class WordEditActivity : BaseActivity<ActivityWordEditBinding, WordEditViewModel>(
    WordEditViewModel::class.java,
    R.layout.activity_word_edit,
    BR.viewModel
) {
    private lateinit var textToImageUtil: TextToImageUtil
    // TODO: 传递到发布的逻辑
    private val color = intArrayOf(Color.RED, Color.GREEN, Color.YELLOW, Color.GRAY, Color.BLUE)
    private val random = Random()
    private var imagePath: Bitmap? = null
    private var colorPosition = 0
    override fun initView() {
        textToImageUtil = TextToImageUtil()
        val word = intent.getStringExtra(putExtraKey.WORD.key)
        mViewDataBinding?.etWord?.setText(word ?: "")
        imagePath = textToImageUtil.createTextImage(
            mViewDataBinding?.etWord?.text.toString(), 378, 450, 50,
            color[colorPosition], null, -1, this, false
        )
        mViewDataBinding?.etWord?.setSelection(
            mViewDataBinding?.etWord?.text.toString().length ?: 0
        )
        mViewDataBinding?.ivPrepImg?.setImageBitmap(imagePath)
//        mViewModel?.editWordSize?.observe(this) {
//            mViewDataBinding?.etWord?.setSelection(it)
//        }
        colorPosition = random.nextInt(color.size)
        mViewModel?.editWord?.observe(this) {
            imagePath = textToImageUtil.createTextImage(
                it, 378, 450, 50,
                color[colorPosition], null, -1, this, false
            )
            mViewDataBinding?.ivPrepImg?.setImageBitmap(imagePath)
        }
        onClick()
    }

    fun onClick() {

        mViewDataBinding?.etWord?.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {
                mViewModel?.setEditWord(p0?.toString() ?: "")
                mViewModel?.setEditWordSize(p0?.length ?: 0)
            }

        })
        mViewDataBinding?.ivBack?.setOnClickListener({
            finish()
        })
        mViewDataBinding?.btnNext?.setOnClickListener({
//            val allPath = Sp.getStringArray(this, SpKey.IMAGE_PATH.key, arrayOf())?.toMutableList()
//            allPath?.add(TextToImageUtil().getBitmapPath()) // 添加新路径
//            Sp.saveStringArray(
//                this,
//                SpKey.IMAGE_PATH.key,
//                allPath?.toTypedArray() ?: arrayOf(TextToImageUtil().getBitmapPath())
//            )
            mViewModel?.jumpToInvitationPublicActivity(textToImageUtil.getBitmapPath())
        })
        mViewDataBinding?.btnKeepPhoto?.setOnClickListener({
            textToImageUtil.saveBitmapToExternalStorage()

            Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show()
        })

        mViewDataBinding?.layoutNone?.setOnClickListener({
            colorPosition = random.nextInt(color.size)
            imagePath = textToImageUtil.createTextImage(
                mViewDataBinding?.etWord?.text.toString(), 378, 450, 50,
                color[colorPosition], null, -1, this, false
            )
            mViewDataBinding?.ivPrepImg?.setImageBitmap(imagePath)
        })
        mViewDataBinding?.layoutEasy?.setOnClickListener({
            var temp = random.nextInt(color.size)
            while (temp == colorPosition) {
                temp = random.nextInt(color.size)
            }
            imagePath = textToImageUtil.createTextImage(
                mViewDataBinding?.etWord?.text.toString(), 378, 450, 50,
                color[colorPosition], null, color[temp], this, false
            )
            mViewDataBinding?.ivPrepImg?.setImageBitmap(imagePath)
        })
        mViewDataBinding?.layoutNone?.setOnClickListener({
            colorPosition = random.nextInt(color.size)
            imagePath = textToImageUtil.createTextImage(
                mViewDataBinding?.etWord?.text.toString(), 378, 450, 50,
                color[colorPosition], null, -1, this, false
            )
            mViewDataBinding?.ivPrepImg?.setImageBitmap(imagePath)
        })
        mViewDataBinding?.layoutEmoji?.setOnClickListener({
            imagePath = textToImageUtil.createTextImage(
                mViewDataBinding?.etWord?.text.toString(), 378, 450, 50,
                color[colorPosition], null, -1, this, true
            )
            mViewDataBinding?.ivPrepImg?.setImageBitmap(imagePath)
        })
    }


}